package com.lajiaomian.springbootruijiwaimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lajiaomian.springbootruijiwaimai.common.BaseContext;
import com.lajiaomian.springbootruijiwaimai.common.R;
import com.lajiaomian.springbootruijiwaimai.pojo.Employee;
import com.lajiaomian.springbootruijiwaimai.service.impl.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @author lajiaomian
 * @create 2022-08-11-10:40
 */
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpServletRequest request){

        //1.将页面提交的密码加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的username查询数据库
        String username = employee.getUsername();
        Employee emp = employeeService.getEmployee(username);

        //3.如果没有查询到则返回失败
        if (emp==null){
            return R.error("登录失败，用户名错误");
        }
        //4.如果查询到了，查看密码是否正确
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败，密码错误");
        }
        //5.查看员工状态，如果是禁用，就返回员工已禁用结果
        if(emp.getStatus()==0){
            return R.error("登录失败，非内部人员");
        }
        //6.登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加后台员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> addEmp(@RequestBody Employee employee,HttpSession session){
        String pwd = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(pwd);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long empId =(Long) session.getAttribute("employee");
        BaseContext.setCurrId(empId); //将empId放入当前线程中
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页展示员工
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        log.info("page = {} ,pageSize = {} ,name = {}",page,pageSize,name);
        Page<Employee> pageInfo = employeeService.getEmployeePage(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        BaseContext.setCurrId(empId); //将empId放入当前线程中
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        //注意此处修改完了对象后传给浏览器，前端的js由于id的后面三位无法识别而四舍五入导致精度损失，
        //所以运行没报错，但修改了没有用，因为修改的id是不正确的，所以等于修改了错误的对象
        //解决办法：添加jacksonObjectMapper的消息转换器文件，处理java对象转json对象。
        return R.success("更新成功");
    }

    /**
     * 获取员工信息
     * @param id
     * @return
     */
    //修改员工时，需要先获取员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("此处调用了");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有此用户");
    }
}
